from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from select import select

from cart.models import Cart, Comments
from cart.ser import CartSer, CommentsSer
from course.models import Course
from course.ser import CourseSer
from order.models import Orders, OrderDetails
from utils.authentication import MyAuthentication


# Create your views here.
class CartView(APIView):
    authentication_classes = [MyAuthentication]

    def post(self, request):
        user = request.data.get('user')
        course = request.data.get('course')
        cart = Cart.objects.filter(user=user, course=course)
        if cart:
            return Response({'msg': '您已经添加过当前商品了', 'code': 401})
        else:
            ser = CartSer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'msg': '添加成功', 'code': 201})
            else:
                return Response({'msg': '添加失败', 'code': 401})


class CartAllView(APIView):
    authentication_classes = [MyAuthentication]

    def get(self, request):
        user = request.query_params.get('user')
        cartlist = Cart.objects.filter(user=user)
        ser = CartSer(cartlist, many=True)
        return Response({'data': ser.data})


class CartUpdateOne(APIView):
    authentication_classes = [MyAuthentication]

    def put(self, request):
        id = request.data.get('course')
        select = request.data.get('selected')
        cart = Cart.objects.get(pk=id)
        if cart:
            cart.selected = select
            cart.save()
            return Response({'msg': 'OK'})
        return Response({'msg': '修改失败'})


class CartUpdateAll(APIView):
    authentication_classes = [MyAuthentication]

    def put(self, request):
        user = request.data.get('user')
        selected = request.data.get('selected')
        Cart.objects.filter(user=user).update(selected=selected)
        return Response({'msg': 'OK'})


class CartDelByuser(APIView):
    authentication_classes = [MyAuthentication]

    def delete(self, request):
        user = request.query_params.get('user')
        Cart.objects.filter(user=user, selected=True).delete()
        return Response({'msg': '删除成功'})


class CartDelByid(APIView):
    authentication_classes = [MyAuthentication]

    def delete(self, request):
        course = request.query_params.get('course')
        Cart.objects.filter(id=course).delete()
        return Response({'msg': '删除成功'})


class CommentsView(APIView):
    def get(self, request):
        course = request.query_params.get('course')
        data = Comments.objects.filter(course=course)
        print(data)
        ser = CommentsSer(data,many=True)
        return Response(ser.data)


class CommentAdd(APIView):
    authentication_classes = [MyAuthentication]

    def post(self, request):
        user = request.data.get('user')
        course = request.data.get('course')
        orderlist = Orders.objects.filter(user=user,status=1)
        orderidlist = []
        for order in orderlist:
            orderidlist.append(order.id)
        orderdetaillist = OrderDetails.objects.filter(order__in=orderidlist)
        print(orderdetaillist)
        print(orderidlist)
        print(course)
        for orderdetail in orderdetaillist:
            if int(course) == orderdetail.courseid_id:
                ser = CommentsSer(data=request.data)
                if ser.is_valid():
                    ser.save()
                    return Response({'msg': '发布成功', 'code': 201})
                else:
                    return Response({'msg': '发布失败', 'code': 401})
        return Response({'msg': '必须购买当前商品才能发布评论', 'code': 401})
